Styling {ggplot2} graphics for accessibility

Nicola Rennie

My R Journey (so far…)

My R Journey (so far…)

A stream graph showing the use of R for the author between 2014 and present day

My {ggplot2} journey (so far…)

A radar chart showing personality traits of Villanelle from Killing Eve A radar chart showing personality traits of Eve from Killing Eve A radar chart showing personality traits of Carolyn from Killing Eve

My {ggplot2} journey (so far…)

A bar chart showing the number of books by the top 10 authors on the New York bestsellers list

My {ggplot2} journey (so far…)

A chart showing the amount of caffeine in different starbucks drinks. Caffeine amount is shown by colour of coffee cup icons.

My {ggplot2} journey (so far…)

A flowchart showing the story of goldilocks

What I do at Jumping Rivers?

Consultancy Hex stickers for four common R packages

Training Cartoon of a teacher at the front of a class with a board showing an R

R Community Shiny in Production conference logo of a robot holding a spanner

Styling {ggplot2} graphics for accessibility


  Fonts


  Colours


  Alt Text

  Fonts

Fonts in {ggplot2}

  • {extrafont}
  • {ragg}
  • {showtext}

Fonts in {ggplot2}


Choose a font: fonts.google.com


Load it into R:

library(showtext)
font_add_google(name = "Bungee Shade", 
                family = "bungee")
showtext_auto()

Fonts in {ggplot2}

ggplot(data = mtcars,
      mapping = aes(x = mpg,
                    y = disp)) +
  geom_point() +
  labs(title = "My plot title")

Fonts in {ggplot2}

ggplot(data = mtcars,
      mapping = aes(x = mpg,
                    y = disp)) +
  geom_point() +
  labs(title = "My plot title") +
  theme(
    plot.title = element_text(
      family = "bungee"))

How do I choose a font?

Arial: Does it pass the 1Il test?


Times New Roman: Does it pass the 1Il test?


Courier New: Does it pass the 1Il test?

  Colours

Colours elements in {ggplot2}

  • Colour of geoms (mapped in aes())

  • Colour of geoms (not mapped in aes())

  • theme() elements e.g. background colour

Defining colours in {ggplot2}

Let’s make an example plot to demonstrate…

name n
Crowned lemur 2094
Ring-tailed lemur 7490
Gray mouse lemur 12275

Group of ring-tailed lemurs Image: Unsplash

Defining colours in {ggplot2}

ggplot(data = lemurs, 
       aes(x = name,
           y = n,
           fill = name)) +
  geom_col()

Bar chart showing the number of lemurs for three different species)

Choosing colours in {ggplot2}

scale_*_brewer(): colorbrewer2.org

ggplot(data = lemurs, 
       aes(x = name,
           y = n,
           fill = name)) +
  geom_col() +
  scale_fill_brewer(
    palette = "Dark2")

Bar chart showing the number of lemurs for three different species)

Choosing colours in {ggplot2}

Colour palette packages: github.com/EmilHvitfeldt/paletteer

library(rcartocolor)
ggplot(data = lemurs, 
       aes(x = name,
           y = n,
           fill = name)) +
  geom_col() +
  scale_fill_carto_d("Prism")

Bar chart showing the number of lemurs for three different species)

Choosing colours in {ggplot2}

Defining your own colours

Choosing colours in {ggplot2}

ggplot(data = lemurs, 
       aes(x = name,
           y = n,
           fill = name)) +
  geom_col() +
  scale_fill_manual(
    values = c("#157145",
               "#4C1E4F",
               "#DE6E4B"))

Bar chart showing the number of lemurs for three different species)

Are your colours accessible?

my_colours = c("#157145", "#4C1E4F", "#DE6E4B")
  • {colorblindcheck}

  • {colorblindr}

{colorblindcheck}

colorblindcheck::palette_check(my_colours)
          name n tolerance ncp ndcp  min_dist mean_dist max_dist
1       normal 3  44.33725   3    3 44.337246  48.67622 55.30187
2 deuteranopia 3  44.33725   3    1 25.872723  34.57704 50.79886
3   protanopia 3  44.33725   3    1  9.345734  31.53395 46.54013
4   tritanopia 3  44.33725   3    2 41.560179  50.49023 60.86996

{colorblindr}

g <- ggplot(data = lemurs, 
            aes(x = name,
                y = n,
                fill = name)) +
  geom_col() +
  scale_fill_manual(
    values = c("#157145",
               "#4C1E4F",
               "#DE6E4B"))

Check it with {colorblindr}:

library(colorblindr)
cvd_grid(g)

{colorblindr}

Grid showing how bar chart looks with different types of colorblindness

  Alt Text

How to write alt text for plots

Screenshot of article explaining how to write alt text for graphs. Link: medium.com/nightingale/writing-alt-text-for-data-visualization-2a218ef43f81

Writing alt text in {ggplot2}

g <- ggplot(data = mtcars,
            mapping = aes(x = mpg,
                          y = disp)) +
  geom_point() +
  labs(alt = "Some of my own alt text.")


Extract the alt text:

get_alt_text(g)
[1] "Some of my own alt text."

Alt text in Quarto

```{r}
#| fig.alt: !expr ggplot2::get_alt_text(g)
#| fig-align: center
#| output-location: slide
g
```


The RMarkdown syntax:

{r, fig.alt=ggplot2::get_alt_text(g)}

Alt text in Quarto

Some of my own alt text.

Something a bit experimental…

remotes::install_github("nrennie/ggalttext")
library(ggalttext)

Something a bit experimental…

g1 <- ggplot(data = mtcars,
            mapping = aes(x = mpg,
                          y = disp)) +
  geom_point()
generate_alt_text(g1)
[1] "A plot with mpg on the x-axis and disp on the y-axis. The data is displayed using points."

Something a bit experimental…

g2 <- ggplot(data = mtcars,
            mapping = aes(x = mpg,
                          y = disp)) +
  geom_point() +
  labs(title = "My plot", 
       subtitle = "A descriptive subtitle.", 
       alt = "Some of my own alt text.")
generate_alt_text(g2)
[1] "My plot. A descriptive subtitle. A plot with mpg on the x-axis and disp on the y-axis. The data is displayed using points. Some of my own alt text."

Something a bit experimental…

```{r}
#| fig.alt: !expr ggalttext::generate_alt_text(g2)
#| fig-align: center
#| output-location: slide
g2
```

Something a bit experimental…

My plot. A descriptive subtitle. A plot with mpg on the x-axis and disp on the y-axis. The data is displayed using points. Some of my own alt text.

Something a bit experimental…

Screenshot of tweet from Hadley Wickham stating better alt text for ggplot2 is being explored.

Resources

{ggplot2}

Accessibility

Questions?